package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.PressRelese;

/**
 * A class used for managing press release
 */
public class PressReleaseDAO {
	
private Database db;

/**
 * Instantiate a new object of this class
 * @param database a database
 */
public PressReleaseDAO(Database db) {
    this.db = db;
}

//get PressRelease by site_id and type = S
public List<Map<String, Object>> findPressReleaseBySiteId(int pnSiteId) {
    String vaSql = "SELECT pin,topic,detail,start_date,end_date,image_url FROM press_release "
    		+ "WHERE"
    		+ " site_id = '"+pnSiteId+"'"
    		+ " AND type='S' "
    		+ " ORDER BY start_date ASC ";
    List<Map<String, Object>> result = db.queryList(vaSql);
    if (result != null) {
        return result;
    } else {
        return null;
    }
}


//get PressRelease by site_id and type = P
public List<Map<String, Object>> findPressReleaseBySiteIdAndTypeP() {
  String vaSql = "SELECT pin,topic,detail,start_date,end_date,image_url FROM press_release WHERE site_id ='0' AND type='P' ORDER BY start_date ASC";
  List<Map<String, Object>> result = db.queryList(vaSql);
  if (result != null) {
      return result;
  } else {
      return null;
  }
}


//insert pressRelease 
public void addPressRelese(PressRelese poPressRelese) {
   	String vaSql = "Insert into PRESS_RELEASE (SITE_ID,PIN,TOPIC,DETAIL,"
   			     + "START_DATE,END_DATE,IMAGE_URL,TYPE,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)"
   			    + "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
   	 
   			try {
   					db.insert(vaSql, poPressRelese.getVnSiteId(),
   								poPressRelese.getVaPin(),
   								poPressRelese.getVaTopic(),
   								poPressRelese.getVaDetail(),
   								poPressRelese.getVaStartDate(),
   								poPressRelese.getVaEndDate(),
   								poPressRelese.getVaImageUrl(),
   								poPressRelese.getVaType(),
   								poPressRelese.getVnInsertUserId(),
   								poPressRelese.getVaInsertDate(),
   								poPressRelese.getVaInsertTime(),
   								poPressRelese.getVnUpdateUserId(),
   								poPressRelese.getVaUpdateDate(),
   								poPressRelese.getVaUpdateTime()
   								
   								);
   							
   			}catch (Exception e) {
   	        	throw new RuntimeException(e);
   	        }
   	}

//update PressRelease  
public void updatePressRelease(int pnSiteId,String paTopic,String paOldStartDate,String paOldEndDate,PressRelese poPressRelese) {
	System.out.println(paOldStartDate+"  "+paOldEndDate);
	String vaSql  = "UPDATE PRESS_RELEASE SET  PIN = ?,"
					+ "TOPIC = ?,"
					+ "DETAIL = ?,"
	    			+ "START_DATE =?, "
	    			+ "END_DATE = ?,"
	    			+ "IMAGE_URL = ?,"
	    			+ "TYPE = ?,"
	    			+ "UPDATE_USER_ID = ?, "
	    			+ "UPDATE_DATE = ?,"
	    			+ "UPDATE_TIME = ? "
	    			+ "WHERE "
	    			+ "SITE_ID ='"+pnSiteId+"'"
	    			+ " AND TOPIC ='"+paTopic+"'"
	    			+ " AND START_DATE  ='"+paOldStartDate+"'"
	    			+ " AND END_DATE  ='"+paOldEndDate+"'";
 try {
		db.update(vaSql,poPressRelese.getVaPin(),
					poPressRelese.getVaTopic(),
					poPressRelese.getVaDetail(),
					poPressRelese.getVaStartDate(),
					poPressRelese.getVaEndDate(),
					poPressRelese.getVaImageUrl(),
					poPressRelese.getVaType(),
					poPressRelese.getVnUpdateUserId(),
					poPressRelese.getVaUpdateDate(),
					poPressRelese.getVaUpdateTime()
					);
		//System.out.println(poPressRelese.getVaStartDate()+"  "+poPressRelese.getVaEndDate());
		db.commit();
}catch (Exception e) {
	throw new RuntimeException(e);
}
	
}
//delete PressRelease 
	public boolean deletePressReleseBySitId(int pnSiteId,String paTopic,String paStartDate ,String paEndDate ) {
	    String vaSql = "DELETE FROM press_release"
	    		+ " WHERE site_id = " + pnSiteId + " "
        		+ " AND start_date = " + paStartDate + " "
        		+ " AND end_date = '" + paEndDate + "' "
        		+ " AND topic like '%" + paTopic + "%' ";
	    try {
	        db.insert(vaSql);
	        db.commit();
	        return true;
	    } catch (Exception e) {
	        return false;
	    }
	}
	//get topic ,detail and image_url By topic and start_date and end_date for TYPE='S' 
	  public Map<String, Object> findPressReleseByTopic(int pnSiteId, String paTopic,String paStatDate,String paEndDate) {
	        String vaSql = "SELECT topic, detail, image_url FROM press_release"
	        		+ " WHERE site_id = " + pnSiteId + " "
	        		+ " AND start_date = " + paStatDate + " "
	        		+ " AND end_date = '" + paEndDate + "' "
	        		+ " AND topic like '%" + paTopic + "%' ";
	        Map<String, Object> result = db.querySingle(vaSql);
	        if (result != null) {
	            return  result;
	        } else {
	            return null;
	        }
	    }
}
